jQuery(document).ready(function($) {
	
	$('select').not('.jq-picker-outer select').each(function(index) {
		try {
			$(this).css('min-width', ($(this).width() +22)+"px" )
			$(this).css('max-width', '100% !important');
		} catch (err) {
			return false;
		}
	});
	
	
	try {
		$('#locale_picker').bind('change', function(event) {
			var location = window.document.location.toString();
			var newLocale = $(this).val();
			if (location.match(/\/[a-z]{2}-[a-z]{2}\//i)) {
				window.document.location = location.replace(/\/[a-z]{2}-[a-z]{2}\//i,"/"+newLocale+"/");
			};
		});
	} catch (err) {
		if(window.console) {
			//window.console.log(err)
		}
	}
	
	
	$('.locales').not('.open').hide();
	
		
		$('.open_locale').each(function(index) {
			$(this).bind("click", function() {
				var id = $(this).attr("href").slice(1);
				$('.locales:eq('+index+')').slideToggle('fast',callback);
				return false;
			});
		});
	
	function callback () {
		return false;
	}
	
	// $('#journal_categories').before("<select id='journal_cat-replaced' value=''/>").hide().find('input').each(function(index) {
	// 	val = $(this).val()
	// 	text = $(this).parent('label').text()
	// 	
	// 	option = "<option value='"+val+"'>"+text+"</option>";
	// 	$("#journal_cat-replaced").append(option);
	// });
	
	// $('#journal_categories .hide').hide(function() {
	// 	$(this).find('input, select, textarea').attr('disabled', 'disabled');
	// });
	// 
	// 
	// $('#journal_categories input').click(function() {
	// 	$(this).parent().parent().find('.hide').show().each(function(index) {
	// 		$(this).find('input, select, textarea').attr('disabled', '')
	// 	});
	// 	
	// 	alert(text)
	// });
	// 
	// 
	
	
try {
	$('.flash').each(function(index) {
		$(this).flash(null, { version: 8 }, function(htmlOptions) {
		        var $this = $(this);
		if ($this.attr('rel') != "") {
	        var params = $this.attr('rel').split(':');
		} else {
			if ($this.attr('width') != "") {params[0] = $this.attr('width')}
			if ($this.attr('height') != "") {params[1] = $this.attr('height')}
		}
		if ($this.attr('href') != "") {
			htmlOptions.src = $this.attr('href');
		} else if ($this.attr('src') != "") {
			htmlOptions.src = $this.attr('src');
		};
		        htmlOptions.width = params[0];
		        htmlOptions.height = params[1];
		        this.innerHTML = '<div class="alt">'+this.innerHTML+'</div>';
		        $this.addClass('flash-replaced').prepend($.fn.flash.transform(htmlOptions));
		    }).click(function() {
		    	return false;
		    });
	});

} catch (err) {
	//if(window.console) { window.console.log(err) }	
	return false;

}

try {
	$('input:submit, input:button').addClass("button")

} catch (err) {
	//if(window.console) { window.console.log(err) }	
	return false;

}
	
	
	
	//a div called hide will be replaced with the contents of its title attribute, which when clicked will load the original hide div with a close button, which when clicked closes the div	
try {
		$(".hide").css("position","relative").each(function(index) { 
			$(".hidden").append(closebutton)

			$('#sidebar .hidden').hide()

			$("#sidebar .description").click(function() {
				$(this).siblings(".hidden").slideDown("fast")
			});
			$("#sidebar .hidden .close").click(function() {
				$(this).parent(".hidden").slideUp("fast")
			});

			$(".description").not("#sidebar .description").click(function() {
				if ($(this).siblings(".hidden").is(":visible")) {
					$(this).removeClass("active").siblings(".hidden").slideUp("fast");
				} else {
					$(this).addClass("active").siblings(".hidden").slideDown("fast");
				}
			});
			$(".hidden .close").not("#sidebar .hidden .close").click(function() {
				$(this).parent(".hidden").slideUp("fast").prev().removeClass("active")
			});
		});

} catch (err) {
	//if(window.console) { window.console.log(err) }	
	return false;
}
			
try {
	initCat()

} catch (err) {
	//if(window.console) { window.console.log(err) }	
	return false;
}
		
try {
		$('#journal_cat-add').click(function() {
			var val = $(this).prev().val()
			if (val != "") {
				addCat(val)
			}
			return false;
		});
		$('#cat_inputs label').not(".main_category").click(function() {
			return false;
		});

} catch (err) {
	//if(window.console) { window.console.log(err) }	
	return false;
}
		


});

function initCat () {
	$('#journal_categories').each(function(index) {
		var inputs = $(this).html();
		inner =  "<option value=''>Select</option>"; // $(this).html()
		
		$("#cat_inputs").html(inputs).children('p').each(function(index) {
			$(this).hide()
			.append('<span class="remove_cat omega grid_2">remove</span>')
			$(this).find('input[name=category[]]').each(function(index) {
				$(this).add($(this).parent().next().children("input")).attr("disabled","disabled");

				if ($(this).val().match("trips")) {
					$(this).addClass("trip")
				};
			});
		});
		
		$(this).find('input[name=category[]]').each(function() {
			if ($(this).is(":checked")) {
				inner = inner + ("<option value='"+ $(this).val() +"' disabled='disabled'>"+$(this).parent('label').text()+"</option>\r")
				addCat($(this).val())
			} else {
				inner = inner + ("<option value='"+ $(this).val() +"'>"+$(this).parent('label').text()+"</option>\r")
			}
		});
		$(this).replaceWith("<select id='journal_categories'>"+ inner +"</select><button id='journal_cat-add'>Add</button>")
	});
	$('.remove_cat').click(function() {
		$(this).parent("p").find('input:checked').each(function() {
			var val = $(this).val()
			var label = $(this).parent('label').text();
			$(this).closest("p").hide()
			.find('input, textarea, select').attr("disabled","disabled")
			addOption(val,label);
		});
		return false;
	});
	
}


function addCat (val) {
	checkOff(val)
	removeOption(val)
}

function checkOff (val) {
	$('#cat_inputs .alpha input[value='+val+']:checkbox').each(function(index) {
		$(this).attr("checked","checked")
		$(this).closest("p").slideDown().find('input, textarea, select').attr("disabled","")
	});
	if ($('.main_category input:checked').size() == 0) {
		$('.main_category input:enabled').parent(".main_category").children("input").attr("checked","checked")
	};
}
function removeOption (val) {
	$('#journal_categories option[value='+val+']').attr("disabled","disabled")
}
function addOption (val, label) {
	$('#journal_categories  option[value='+val+']').attr("disabled","")
}


function setIframeBgImg (obj,val) {
	if ($(obj).size() < 1) {
		setTimeout(function() {setIframeBgImg (obj,val)}, 1000);
	} else {
		jQuery(obj).contents().find("html").css('background-image', 'url('+val+')');
	}
}



